﻿package rd.hardcore.selector
{
	import adobe.utils.CustomActions;
	import caurina.transitions.Tweener;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import rd.hardcore.Datas;
	import rd.hardcore.Main;
	import rd.hardcore.sampler.PadEvent;
	
	/**
	 * ...
	 * @author Romain Prache
	 */
	public class Selector extends MovieClip
	{
		
		/* VAR */
		static public var currentSong : int = 0;

		public var datas		: XML;
		public var bt			: MovieClip;
		public var tf  			: TextField;
		public var bg			: Sprite;
		
		private var loader		: URLLoader;
		private var tabItems	: Array = [];
		private var scrollList	: ScrollList;
		private var listOpen 	: Boolean	=	false;
		
		/* Selector */
		public function Selector()
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		
		/* Init */
		private function init ( evt : Event ) : void
		{		
			trace("Selector--");
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, kill);
			
			bt.addEventListener(MouseEvent.ROLL_OVER, 	handleOver);
			bt.addEventListener(MouseEvent.ROLL_OUT, 	handleOut);
			bt.addEventListener(MouseEvent.CLICK, 		handleClick);
			
			bt.buttonMode = true;
			bt.mouseChildren = false;
			
			scrollList = new ScrollList();
			addChild(scrollList);
			
			addEventListener(PadEvent.ITEM_SELECTED, itemSelectedHandler);
		}
		
		private function itemSelectedHandler(e:PadEvent):void 
		{
			tf.htmlText = e.label;
			Main.song = e.mediaPath;
			handleClick();
		}
		
		private function handleOver(e:MouseEvent):void 
		{
			bt.alpha = .8;
		}
		
		private function handleOut(e:MouseEvent):void 
		{
			bt.alpha = 1;
		}
		
		private function handleClick(e:MouseEvent = null):void 
		{
			if (listOpen) {
				scrollList.setMask(0);
				Tweener.addTween(bg,{height	: 26, time:1});
			}else {
				scrollList.setMask(SelectList.HEIGHT);
				Tweener.addTween(bg,{height	: SelectList.HEIGHT, time:1});
			}
			
			listOpen = !listOpen;
		}
		
		
		/* Kill */
		private function kill ( evt : Event ) : void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, kill);
		}
		
	}
	
}